﻿using System;
using System.IO;
using System.Collections.Generic;
using UnityEngine;

namespace Z1029.NoteEditor
{
    public class ChartFormatter : MonoBehaviour
    {
        public void Save()
        {
            FindObjectOfType<NoteGlobalView.ChartFormater>().BuildChart();
            /*LevelController.chart = ConvertToGVJson();
            FileStream fs = new FileStream(Path.Combine(Path.GetDirectoryName(Application.dataPath), "Project//Chart_TMP.json"), FileMode.Create, FileAccess.Write);
            byte[] bs = System.Text.Encoding.UTF8.GetBytes(LevelController.chart);
            fs.Write(bs, 0, bs.Length);
            fs.Close();
            print("OK!");*/
        }

        [Obsolete("请使用GlobalView.ChartFormatter.Save()")]
        public static string ConvertToGVJson()
        {
            string path = Path.Combine(Path.GetDirectoryName(Application.dataPath), "Project//Chart.json");
            //Z1029.NoteGlobalView.Chart c = JsonUtility.FromJson<Z1029.NoteGlobalView.Chart>(File.ReadAllText(path,System.Text.Encoding.UTF8));
            Z1029.NoteGlobalView.Chart c = Z1029.NoteGlobalView.PhigrosChartReader.ReadChart(File.ReadAllText(path, System.Text.Encoding.UTF8));
            int index = Z1029.NoteGlobalView.JudgementLine.selectedJudgeline.index;
            Z1029.NoteGlobalView.JudgeLineSet jls = new NoteGlobalView.JudgeLineSet();
            JudgementLine js = LevelController.instance.judgelines[0];
            /*LevelController.instance.editMode = LevelController.EditMode.Event;
            LevelController.instance.ChangeMode();
            EventBase[] ebs = FindObjectsOfType<EventBase>();
            if (ebs.Length == 0)
                goto NoteGenerate;
            js.alphas = null;
            js.rotates = null;
            js.moves = null;
            /oreach (var eb in ebs)
            {
                if(eb.type == EventType.Alpha)
                {
                    js.alphas += eb.eventData.value;
                }
            }*/
            jls.alphaEvents = js.alphas;
            jls.rotateEvents = js.rotates;
            jls.moveEvents = js.moves;

            NoteGenerate:
            LevelController.instance.editMode = LevelController.EditMode.Note;
            LevelController.instance.ChangeMode();
            Note[] notes = FindObjectsOfType<Note>();
            List<NoteGlobalView.SpeedEvent> lsdjj = new List<NoteGlobalView.SpeedEvent>();
            for (int i = 0; i < js.speeds.Count; i++)
            {
                NoteGlobalView.SpeedEvent sp = new NoteGlobalView.SpeedEvent() { startTime = js.speeds[i].startTime, endTime = js.speeds[i].endTime, value = js.speeds[i].value };
                lsdjj.Add(sp);
            }
            jls.speedEvents = lsdjj.ToArray();
            jls.bpm = js.bpm;
            List<Z1029.NoteGlobalView.Note.NoteData> aboves = new List<NoteGlobalView.Note.NoteData>();
            List<Z1029.NoteGlobalView.Note.NoteData> belows = new List<NoteGlobalView.Note.NoteData>();
            for (int i = 0; i < notes.Length; i++)
            {
                Z1029.NoteGlobalView.Note.NoteData nd = new Z1029.NoteGlobalView.Note.NoteData();
                nd.type = (Z1029.NoteGlobalView.NoteType)notes[i].noteData.type;
                nd.beatTime = notes[i].noteData.beatTime;
                nd.beatHoldTime = notes[i].noteData.beatHoldTime;
                nd.speed = notes[i].noteData.speed;
                nd.positionX = notes[i].noteData.positionX;
                nd.time = Utils.GetTimeFromBeatTime(nd.beatTime, jls.bpm);
                nd.holdTime = Utils.GetTimeFromBeatTime(nd.beatHoldTime, jls.bpm);
                nd.index = 0;
                if (notes[i].direction == Note.Direction.Above)
                {
                    aboves.Add(nd);
                }
                else
                {
                    belows.Add(nd);
                }
            }
            jls.notesAbove = aboves.ToArray();
            jls.notesBelow = belows.ToArray();
            c.numOfNotes += notes.Length;
            c.judgeLineList[index] = jls;
            string json = JsonUtility.ToJson(c, true);

            //print(json);
            return json;
        }
    }
}